package com.sale.succ.system.ftp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FTPDataSocket {
	/**
	 * The underlying socket for Active connection.
	 */
	private ServerSocket activeSocket = null;

	/**
	 * The underlying socket for PASV connection or Socket acepted from server.
	 */
	private Socket passiveSocket = null;

	/**
	 * Create socket wrapper for Active connection.
	 */
	FTPDataSocket(ServerSocket s) {
		activeSocket = s;
	}

	/**
	 * Create socket pper for PASV connection.
	 */
	FTPDataSocket(Socket s) {
		passiveSocket = s;
	}

	/**
	 * Set the TCP timeout on the underlying control socket.
	 * <p/>
	 * If a timeout is set, then any operation which takes longer than the
	 * timeout value will be killed with a java.io.InterruptedException.
	 * 
	 * @param millis
	 *            The length of the timeout, in milliseconds
	 */
	void setTimeout(int millis) throws IOException {

		if (passiveSocket != null)
			passiveSocket.setSoTimeout(millis);
		else if (activeSocket != null)
			activeSocket.setSoTimeout(millis);
	}

	/**
	 * If active mode, accepts the FTP server's connection - in PASV, we are
	 * already connected. Then gets the output stream of the connection
	 * 
	 * @return output stream for underlying socket.
	 */
	OutputStream getOutputStream() throws IOException {

		if (passiveSocket != null) {
			return passiveSocket.getOutputStream();
		} else {
			// accept socket from server, in Active mode
			passiveSocket = activeSocket.accept();
			// get and return its OutputStream
			return passiveSocket.getOutputStream();
		}
	}

	/**
	 * If active mode, accepts the FTP server's connection - in PASV, we are
	 * already connected. Then gets the input stream of the connection
	 * 
	 * @return input stream for underlying socket.
	 */
	InputStream getInputStream() throws IOException {

		if (passiveSocket != null) {
			return passiveSocket.getInputStream();
		} else {
			// accept socket from server, in Active mode
			passiveSocket = activeSocket.accept();
			// get and return it's InputStream
			return passiveSocket.getInputStream();
		}
	}

	/**
	 * Closes underlying sockets.
	 */
	void close() throws IOException {

		if (passiveSocket != null)
			passiveSocket.close();
		if (activeSocket != null)
			activeSocket.close();
	}
}